package com.views
{
	import com.GameModel;
	import com.config.Level;
	import com.controllers.GameController;
	import com.datas.vo.ItemVO;
	import com.event.GameEvent;
	import com.greensock.TweenLite;
	import com.views.objects.GameItemView;
	
	import flash.display.Shape;
	import flash.display.Sprite;
	import flash.utils.Dictionary;

	public class GameRandomLayerView extends Sprite
	{
		private var _ctrl:GameController;
		
		private var g:Shape;
		
		private var itemView:GameItemView;
		private var itemView2:GameItemView;
		private var itemVO:ItemVO;
		private var itemVO2:ItemVO;
		private var isCreate:Boolean = true;
		public function GameRandomLayerView(p_ctrl:GameController)
		{
			super();
			init();
			_ctrl = p_ctrl;
		}
		
		private function init():void{
			//random的背景
			if(!g)g = new Shape;
			
			g.graphics.beginFill(0xffff00);
			g.graphics.drawRect(108,108,206,206);
			g.graphics.endFill();
			g.alpha = 0.3
			
				this.mouseChildren = false;
				this.mouseEnabled = false;
				if(!itemView)itemView = new GameItemView();
				if(!itemView2)itemView2 = new GameItemView();
				this.addChild(itemView);
				itemView.visible = false;
				this.addChild(itemView2);
				itemView2.visible = false;
				itemVO = new ItemVO;
				itemVO2 = new ItemVO;
				itemVO.id = 2;
				itemVO2.id = 2;
				
				this.addChild(g)
		}
		
		public function createView():void{
			var obj:Object = Level.level
			var tempType:int = obj[1].typeCount
			
			var typeRandom:int = int( Math.random()*obj[1].typeCount)+1;
			
			itemView.visible = true;
			itemView2.visible = true;
			
			itemVO.type =typeRandom
			itemView.itemVO = itemVO;
			itemView.move(32,425)
			itemView.show()
			
			itemVO2.type =typeRandom
			itemView2.itemVO = itemVO2;
			itemView2.move(348,425);
			itemView2.show();
			
			createRowCol(itemVO);
			createRowCol(itemVO2);
			
			TweenLite.to(itemView,1,{x:195,y:200,alpha:0});
			TweenLite.to(itemView2,1,{x:195,y:200,alpha:0,onComplete:handleComplete});
		}
		
		private function handleComplete():void{
			//显示在主场景中id为2不可点击  颜色什么的 有区别
			this._ctrl.randomLayerController.updatePlayLayerView(itemVO,itemVO2);

			if(!isCreate){//停止生成、游戏结束
				dispatchEvent(new GameEvent(GameEvent.GAME_OVER_EVENT))
				return;
			}
			if(!GameModel.getInstance().isCreateRandom){//停止生成、随机生成的可点击
				this._ctrl.randomLayerController.updateId();
				stopCreateRandomView();	
				return;
			}
			var obj:Object = Level.level
			var typeRandom:int = int( Math.random()*obj[1].typeCount)+1;
			itemView.move(32,425);
			itemView.alpha = 1;
			itemVO.type =typeRandom
			itemView.itemVO = itemVO;
			itemView.show();
			
			itemView2.move(348,425);
			itemView2.alpha = 1;
			itemVO2.type =typeRandom
			itemView2.itemVO = itemVO;
			itemView2.show();
			
			createRowCol(itemVO)
			createRowCol(itemVO2)

			TweenLite.to(itemView,1,{x:195,y:200,alpha:0});
			TweenLite.to(itemView2,1,{x:195,y:200,alpha:0,onComplete:handleComplete});
		}
		private function stopCreateRandomView():void{
			g.visible = false;
		}
		private function createRowCol(vo:ItemVO):void{
			var count:int = GameModel.getInstance().idCount-44;
			if(getDicCount()>=count){
				isCreate = false;
				return;
			}
			var tempVo:ItemVO = vo;
			var tempRow:int = Math.random()*(Level.ROW-6)+3;
			var tempCol:int = Math.random()*(Level.COL-6)+3;
			var temp:String = String(tempRow)+"," + String(tempCol)+",,";
			var dic:Dictionary = GameModel.getInstance().itemVODic;
			if(dic[temp] == null){
				tempVo.row = tempRow;
				tempVo.col = tempCol;
				dic[temp] = temp;
			}else{
				createRowCol(tempVo);
			}
		}
		private function getDicCount():int{
			var temp:int = 0;
			for each(var str:String in GameModel.getInstance().itemVODic){
				if(str)temp++;
			}
			return temp;
		}
		
	}
}